<template>
  <div class="table-box">
    <Table ref="tableRef" row-key="deploymentId" :columns="columns" :request-api="camundaApi.deploymentList">
      <!-- 菜单操作 -->
      <template #action="{ row }">
        <TableAction :actions="getActions(row)" :show-num="0"></TableAction>
      </template>
    </Table>
  </div>
</template>

<script setup lang="tsx" name="MicroTaskProcess">
import { nextTick, ref } from "vue";
import { Table, TableAction } from "@/components/Table";
import { ActionItem } from "@/components/Table/type";
import camundaApi from "@/views/micro/api/camunda.api";
import { columns } from "./data";
import { useRouter } from "vue-router";
const tableRef = ref();
const router = useRouter();
const getActions = (row): Partial<ActionItem>[] => {
  return [
    {
      type: "primary",
      label: "启动",
      ifShow: !row.suspended,
      onClick: () => {
        router.push({
          path: "/micro/render/process",
          query: { processDefinitionId: row.processDefinitionId, title: row["deploymentName"] }
        });
      }
    },
    {
      type: "danger",
      label: "挂起",
      ifShow: !row["suspended"],
      onClick: async () => {
        await camundaApi.suspendProcessDefinitionById(row.processDefinitionId);
        row.suspended = true;
      }
    },
    {
      type: "primary",
      label: "激活",
      ifShow: !!row["suspended"],
      onClick: async () => {
        await camundaApi.activateProcessDefinitionById(row.processDefinitionId);
        row.suspended = false;
      }
    },
    {
      type: "primary",
      label: "版本",
      ifShow: row.showVersion !== false,
      onClick: async () => {
        if (!row.children) {
          let processDefinitionList = await camundaApi.processDefinitionListByKey(row.processDefinitionKey);
          for (const processDefinition of processDefinitionList) {
            processDefinition.showVersion = false;
          }
          row.children = processDefinitionList.filter(
            processDefinition => processDefinition["deploymentId"] !== row["deploymentId"]
          );
        }
        await nextTick();
        tableRef.value.element.toggleRowExpansion(row, true);
      }
    }
  ];
};
</script>
